package com.bckj.fastboot.upms.biz.mapper;

import com.bckj.fastboot.core.sql.StatementBuilder;
import com.bckj.fastboot.upms.api.entity.SysMenu;
import com.bckj.fastboot.upms.api.vo.SysMenuVO;
import com.bckj.fastboot.upms.api.dto.SysMenuQuery;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;

/**
 * 菜单表(SysMenu)表数据库访问层
 *
 * @author wfc
 * @since 2023-09-14 10:11:55
 */
@Mapper
public interface SysMenuMapper extends BaseMapper<SysMenu> {

    /**
      * 列表
      *
      * @param query req
      * @return list
      */
    @SelectProvider(method = "list" , type = SysMenuProvider.class)
    List<SysMenuVO> list(SysMenuQuery query);

    @SelectProvider(type = SysMenuProvider.class, method = "listMenu")
    List<SysMenu> listMenu(SysMenuQuery query);
    
    class SysMenuProvider{
        
        public String list(SysMenuQuery query){
            return new SQL(){{
                SELECT("id, client_id, menu_name, parent_id, router_path, component, is_frame, is_cache, is_delete, menu_type, visible, status, perms, icon, remark, create_time, update_time, menu_img, business_type, sort, title, redirect, active_menu, key_index, bit_location, perms_type");
                FROM("sys_menu");
                ORDER_BY("id DESC");
            }}.toString();
        }

        public String listMenu(SysMenuQuery query) {
            boolean isAdmin = query.getIsAdmin() != null && query.getIsAdmin() == 1;
            return new StatementBuilder(SysMenu.class, "sm")
                    .select(SysMenu.class, "id,menu_name,parent_id,router_path,component,is_frame,is_cache,menu_type,visible,status,perms,icon,menu_img,sort,title,redirect,active_menu,key_index,bit_location,perms_type")
                    .from(SysMenu.class)
                    .eq(SysMenu::getIsDelete, 0)
                    .eq(SysMenu::getPermsType, query.getPermsType())
                    .eq(query.getId() != null, SysMenu::getStatus, 0)
                    .eq(SysMenu::getIsAdmin, isAdmin ? 1 : 0)
                    .orderByAsc(SysMenu::getSort, SysMenu::getId)
                    .toString();
        }

    }
}

